﻿using HKB.Common;
using HKB.Data;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using ucApi;

public partial class Admin_Admin_AddPassport : Admin, IRequiresSessionState
{
    private UserBll _user;
    private GroupBll _group;
    private string m_Action;
    protected int UserID = 0;
    private DataRow userRs = null;
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax aplicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected UserBll m_User
    {
        get
        {
            UserBll user;
            if (this._user == null)
            {
                this._user = new UserBll(this.conn);
                user = this._user;
            }
            else
            {
                user = this._user;
            }
            return user;
        }
    }
    protected GroupBll m_Group
    {
        get
        {
            GroupBll group;
            if (this._group == null)
            {
                this._group = new GroupBll(this.conn);
                group = this._group;
            }
            else
            {
                group = this._group;
            }
            return group;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        base.Response.Expires = 0;
        this.UserID = Fetch.GetID("UserID");
        if (!base.admin.CheckAdminPower(6))
        {
            base.WriteLog("View", Language.GetAdminMsg("Log/Access", this.UserID.ToString()), "PassPort");
            base.WriteRightErrMsg();
        }
        this.m_Action = Fetch.Get("Action");
        string action = this.m_Action;
        if (action != null)
        {
            if (action == "Add")
            {
                this.AddPanel.Visible = true;
                this.EditPanel.Visible = false;
                this.ShowPanel.Visible = false;
                this.AddBind();
                return;
            }
            if (action == "Modify")
            {
                this.AddPanel.Visible = false;
                this.EditPanel.Visible = true;
                this.ShowPanel.Visible = false;
                this.EditBind();
                return;
            }
            if (action == "Show")
            {
                this.AddPanel.Visible = false;
                this.EditPanel.Visible = false;
                this.ShowPanel.Visible = true;
                this.ShowBind();
                return;
            }
        }
        this.WriteErrMsg("", Language.GetAdminMsg("Common/LostParam", "Action"), "");
    }
    protected void AddBind()
    {
        if (!base.IsPostBack)
        {
            DataTable groupsNameID = this.m_Group.GetGroupsNameID();
            foreach (DataRow dataRow in groupsNameID.Rows)
            {
                this.Add_UserGroupName.Items.Add(new ListItem(dataRow["GroupName"].ToString(), dataRow["GroupID"].ToString()));
            }
            this.Add_UserQuestion.DataSource = this.GetQuestionList();
            this.Add_UserQuestion.DataBind();
        }
    }
    protected void Add_OnClick(object sender, EventArgs e)
    {
        string text = this.Add_UserName.Text;
        string text2 = this.Add_Email.Text;
        string text3 = this.Add_UserPassword.Text;
        string text4 = "";
        string answer = "";
        AppSetings appSetings = new AppSetings();
        if (appSetings.GetConfig("UC_ONorOFF") == "ON")
        {
            int num = Func.uc_user_checkname(text);
            if (num == -1)
            {
                this.WriteErrMsg("", "用户名不合法", "javascript:{window.close();}");
            }
            else
            {
                if (num == -2)
                {
                    this.WriteErrMsg("", "包含要允许注册的词语", "javascript:{window.close();}");
                }
                else
                {
                    if (num == -3)
                    {
                        this.WriteErrMsg("", "用户名已经存在", "javascript:{window.close();}");
                    }
                }
            }
        }
        else
        {
            if (this.m_User.CheckUserExist("UserName", text))
            {
                this.WriteErrMsg("", Language.GetAdminMsg("PassPort/UserNameExist"), "javascript:{window.close();}");
            }
        }
        if (this.config["EnableEmailOnce"].ToString() == "0")
        {
            if (appSetings.GetConfig("UC_ONorOFF") == "ON")
            {
                int num2 = Func.uc_user_checkemail(text2);
                if (num2 == -4)
                {
                    this.WriteErrMsg("", Language.GetAdminMsg("PassPort/EmaileError"), "javascript:{window.close();}");
                }
                else
                {
                    if (num2 == -5)
                    {
                        this.WriteErrMsg("", Language.GetAdminMsg("PassPort/EmaileNO"), "javascript:{window.close();}");
                    }
                    else
                    {
                        if (num2 == -6)
                        {
                            this.WriteErrMsg("", Language.GetAdminMsg("PassPort/EmaileExist"), "javascript:{window.close();}");
                        }
                    }
                }
            }
            else
            {
                if (this.m_User.CheckUserExist("Email", text2))
                {
                    this.WriteErrMsg("", Language.GetAdminMsg("PassPort/EmaileExist"), "javascript:{window.close();}");
                }
            }
        }
        if (this.Add_UserPassword.Text != "")
        {
            text4 = this.Md5(this.Add_UserPassword.Text, 32).ToLower();
        }
        else
        {
            this.WriteErrMsg("", "密码空！", "");
        }
        if (this.Add_Answer.Text != "")
        {
            answer = this.Md5(this.Add_Answer.Text, 32).ToLower();
        }
        int groupID = Convert.ToInt32(this.Add_UserGroupName.SelectedValue.ToString());
        string userIp = Fetch.UserIp;
        if (this.Add_UserEP.Text == "")
        {
            this.Add_UserEP.Text = "0";
        }
        int userID = 0;
        bool flag;
        if (appSetings.GetConfig("UC_ONorOFF") == "ON")
        {
            int userID2 = Func.uc_user_register(text, text3.Trim(), text2);
            flag = this.m_User.AddUser(userID2, text, text4, text2, this.Add_UserQuestion.SelectedValue, answer, Convert.ToInt32(this.Add_Sex.SelectedValue.ToString()), groupID, Convert.ToInt32(this.Add_UserEP.Text), Convert.ToInt32(this.Add_IsLocked.SelectedValue.ToString()), Convert.ToDecimal(this.Add_Balance.Text), userIp);
        }
        else
        {
            try
            {
                DataTable dataTable = DbHelperSQL.GetDataTable("select top 1 max(UserID) as UserID from dbo.HL_User");
                if (dataTable.Rows[0]["UserID"] == null || dataTable.Rows.Count <= 0)
                {
                    userID = 1;
                }
                else
                {
                    userID = Convert.ToInt32(dataTable.Rows[0]["UserID"]) + 1;
                }
            }
            catch (Exception)
            {
                throw;
            }
            flag = this.m_User.AddUser(userID, text, text4, text2, this.Add_UserQuestion.SelectedValue, answer, Convert.ToInt32(this.Add_Sex.SelectedValue.ToString()), groupID, Convert.ToInt32(this.Add_UserEP.Text), Convert.ToInt32(this.Add_IsLocked.SelectedValue.ToString()), Convert.ToDecimal(this.Add_Balance.Text), userIp);
        }
        if (flag)
        {
            base.WriteLog("AddNew", Language.GetAdminMsg("Log/ObjectID", "0"), "PassPort");
            if (this.config["HLIntegration"].ToString() == "1")
            {
                new Integrate(this.conn)
                {
                    ComeUrl = "javascript:{window.returnValue=true;window.close();}",
                    sendEnd = "<script>alert('" + Language.GetAdminMsg("PassPort/SynAdd") + "');location.href='{#GoUrl#}'</script>"
                }.Send("Add", text, text4, text2, this.Add_UserQuestion.Text, answer, 0, "", "");
            }
            else
            {
                this.WriteSuccessMsg("", Language.GetAdminMsg("Common/AddSuccess"), "javascript:{window.returnValue=true;window.close();}");
            }
        }
        else
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/AddFail", "未知"), "javascript:{window.close();}");
        }
    }
    protected void EditBind()
    {
        if (this.UserID == 0)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/LostParam", "UserID"), "");
        }
        if (!base.IsPostBack)
        {
            DataTable groupsNameID = this.m_Group.GetGroupsNameID();
            foreach (DataRow dataRow in groupsNameID.Rows)
            {
                this.Edit_UserGroupName.Items.Add(new ListItem(dataRow["GroupName"].ToString(), dataRow["GroupID"].ToString()));
            }
            DataTable userByUserID = this.m_User.GetUserByUserID(this.UserID);
            if (userByUserID.Rows.Count != 1)
            {
                this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "");
            }
            else
            {
                this.userRs = userByUserID.Rows[0];
                this.LabUserName.Text = this.userRs["UserName"].ToString();
                this.Edit_UserPassword.Text = this.userRs["Password"].ToString();
                this.Edit_Email.Text = this.userRs["Email"].ToString();
                this.Edit_UserQuestion.DataSource = this.GetQuestionList();
                this.Edit_UserQuestion.DataBind();
                this.Edit_UserQuestion.SelectedValue = this.userRs["Question"].ToString();
                if (this.Edit_UserQuestion.SelectedValue != this.userRs["Question"].ToString())
                {
                    this.Edit_UserQuestion.SelectedItem.Selected = false;
                    ListItem listItem = new ListItem(this.userRs["Question"].ToString());
                    listItem.Selected = true;
                    this.Edit_UserQuestion.Items.Add(listItem);
                }
                this.Edit_Answer.Text = this.userRs["Answer"].ToString();
                this.Edit_Sex.SelectedValue = this.userRs["Sex"].ToString();
                this.Edit_IsLocked.SelectedValue = this.userRs["IsLocked"].ToString();
                this.Edit_UserGroupName.Text = this.userRs["GroupID"].ToString();
                this.Edit_UserEP.Text = this.userRs["UserEP"].ToString();
                this.Edit_Balance.Text = this.userRs["Balance"].ToString();
            }
        }
    }
    protected void Edit_OnClick(object sender, EventArgs e)
    {
        bool flag = false;
        string text = "";
        string answer = "";
        string text2 = "";
        string text3 = this.Edit_UserPassword.Text;
        string originPassword = "";
        string username = "";
        IDataReader reader = this.conn.GetReader("Select UserName,Email,PassWord,Answer from [HL_User] Where UserID=" + this.UserID);
        if (reader.Read())
        {
            answer = reader["Answer"].IsNull(new object[0]);
            text2 = (originPassword = reader["PassWord"].IsNull(new object[0]));
            text = reader["Email"].IsNull(new object[0]);
            username = reader["UserName"].IsNull(new object[0]);
        }
        else
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "javascript:{window.close();}");
        }
        reader.Close();
        reader.Dispose();
        AppSetings appSetings = new AppSetings();
        if (this.config["EnableEmailOnce"].ToString() == "0")
        {
            if (text != this.Edit_Email.Text)
            {
                if (appSetings.GetConfig("UC_ONorOFF") == "ON")
                {
                    int num = Func.uc_user_checkemail(this.Edit_Email.Text);
                    if (num == -4)
                    {
                        this.WriteErrMsg("", Language.GetAdminMsg("PassPort/EmaileError"), "javascript:{window.close();}");
                    }
                    else
                    {
                        if (num == -5)
                        {
                            this.WriteErrMsg("", Language.GetAdminMsg("PassPort/EmaileNO"), "javascript:{window.close();}");
                        }
                        else
                        {
                            if (num == -6)
                            {
                                this.WriteErrMsg("", Language.GetAdminMsg("PassPort/EmaileExist"), "javascript:{window.close();}");
                            }
                        }
                    }
                }
                else
                {
                    if (this.m_User.CheckUserExist("Email", this.Edit_Email.Text))
                    {
                        this.WriteErrMsg("", Language.GetAdminMsg("PassPort/EmaileExist"), "javascript:{window.close();}");
                    }
                }
            }
        }
        text = this.Edit_Email.Text;
        if (this.Edit_UserPassword.Text != "")
        {
            text2 = this.Md5(this.Edit_UserPassword.Text, 32).ToLower();
            flag = true;
        }
        if (this.Edit_Answer.Text != "")
        {
            answer = this.Md5(this.Edit_Answer.Text, 32).ToLower();
            flag = true;
        }
        if (this.Edit_UserEP.Text == "")
        {
            this.Edit_UserEP.Text = "0";
        }
        DataTable userByUserID = this.m_User.GetUserByUserID(this.UserID);
        if (userByUserID.Rows.Count != 1)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "");
        }
        int num2 = userByUserID.Rows[0]["UserEP"].ToInt32(new int[0]);
        int num3 = this.Edit_UserEP.Text.ToInt32(new int[0]) - num2;
        int groupID = this.Edit_UserGroupName.SelectedValue.ToInt32(new int[0]);
        this.m_User.UpdateAccountDetails(this.UserID, this.Edit_Balance.Text.ToDecimal2());
        if (appSetings.GetConfig("UC_ONorOFF") == "ON")
        {
            Func.uc_user_edit(username, "", text3, text, true);
        }
        if (this.m_User.UpdateUser(this.UserID, text2, text, this.Edit_UserQuestion.Text, answer, this.Edit_Sex.SelectedValue.ToInt32(new int[0]), groupID, this.Edit_UserEP.Text.ToInt32(new int[0]), this.Edit_IsLocked.SelectedValue.ToInt32(new int[0]), this.Edit_Balance.Text.ToDecimal2()))
        {
            if (num3 > 0)
            {
                int num4 = this.conn.Execute(string.Concat(new object[]
				{
					"insert into HL_UserExpRecorde([UserID],[UserEP],[CType],[CreateTime],[Remark]) values(",
					this.UserID,
					",",
					num3,
					",15,getdate(),'后台增加')"
				}));
            }
            else
            {
                int num4 = this.conn.Execute(string.Concat(new object[]
				{
					"insert into HL_UserExpRecorde([UserID],[UserEP],[CType],[CreateTime],[Remark]) values(",
					this.UserID,
					",",
					num3,
					",15,getdate(),'后台扣除')"
				}));
            }
            base.WriteLog("Modify", Language.GetAdminMsg("Log/ObjectID", this.UserID.ToString()), "PassPort");
            if (flag && this.config["HLIntegration"].ToString() == "1")
            {
                new Integrate(this.conn)
                {
                    ComeUrl = "javascript:{window.returnValue=true;window.close();}",
                    sendEnd = "<script>alert('" + Language.GetAdminMsg("PassPort/SynModify") + "');location.href='{#GoUrl#}'</script>"
                }.Send("Modify", this.LabUserName.Text, text2, text, this.Edit_UserQuestion.Text, answer, 0, originPassword, "");
            }
            else
            {
                this.WriteSuccessMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_Passport.aspx?MenuName=&#x4F1A;&#x5458;&FileName=&#x901A;&#x884C;&#x8BC1;");
            }
        }
        else
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/EditFail", "未知"), "javascript:{window.close();}");
        }
    }
    protected void ShowBind()
    {
        if (this.UserID == 0)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/LostParam", "UserID"), "");
        }
        DataTable userByUserID = this.m_User.GetUserByUserID(this.UserID);
        if (userByUserID.Rows.Count != 1)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/NotData"), "");
        }
        else
        {
            this.userRs = userByUserID.Rows[0];
            this.UserName.Text = this.userRs["UserName"].ToString();
            this.UserPassword.Text = this.userRs["Password"].ToString();
            this.Email.Text = this.userRs["UserName"].ToString();
            this.UserQuestion.Text = this.userRs["Question"].ToString();
            this.Answer.Text = this.userRs["Answer"].ToString();
            this.Sex.Text = ((this.userRs["Sex"].ToString() == "1") ? "女" : ((this.userRs["Sex"].ToString() == "0") ? "男" : "保密"));
            IDataReader reader = this.conn.GetReader("Select GroupName from [HL_Group] Where GroupID=" + this.userRs["GroupID"].ToString());
            if (reader.Read())
            {
                this.UserGroupName.Text = (string)reader["GroupName"];
            }
            reader.Close();
            reader.Dispose();
            this.UserEP.Text = this.userRs["UserEP"].ToString();
            this.Balance.Text = this.userRs["Balance"].ToString();
        }
    }
}
